//
//  YHLoopScrollView.m
//  cunli
//
//  Created by cunli.cn on 15/9/17.
//  Copyright (c) 2015年 cunli.cn. All rights reserved.
//

#import "YHLoopScrollView.h"
#import "UIImageView+WebCache.h"
@interface YHLoopScrollView ()
{
    NSInteger _times;
}
@end

@implementation YHLoopScrollView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _times = 0;
        _imagesGroup = [NSMutableArray array];
        [self addSubview:self.loopScrollView];
        [self addSubview:self.pageControl];
    }
    return self;
}

-(void)setImagesGroup:(NSMutableArray *)imagesGroup
{
    if (imagesGroup.count > 0) {
        id firstObject = [imagesGroup firstObject];
        id lastObject = [imagesGroup lastObject];
        //把源数组第一张图片拷贝一份放新数组末尾
        [imagesGroup insertObject:firstObject atIndex:imagesGroup.count];
        
        //把源数组最后一张图片拷贝一份放新数组开头
        [imagesGroup insertObject:lastObject atIndex:0];
        _imagesGroup = imagesGroup;
        [self reloadLoopScrollView];
    }
    
}

-(UIScrollView *)loopScrollView
{
    if (_loopScrollView == nil) {
        _loopScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        [_loopScrollView setBackgroundColor:[UIColor clearColor]];
        [_loopScrollView setDelegate:self];
        [_loopScrollView setPagingEnabled:YES];
        [_loopScrollView setShowsHorizontalScrollIndicator:NO];
        [_loopScrollView setShowsVerticalScrollIndicator:NO];
        [_loopScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
        UITapGestureRecognizer *loopTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loopScrollViewDidSelectd)];
        [_loopScrollView addGestureRecognizer:loopTap];
        
    }
    return _loopScrollView;
}

-(UIPageControl *)pageControl
{
    if (_pageControl == nil) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((self.frame.size.width-100)/2.0, self.frame.size.height-30, 100, 20)];
        _pageControl.currentPageIndicatorTintColor = YHCurrentPageIndicatorTintColor;
        _pageControl.pageIndicatorTintColor = YHPageIndicatorTintColor;
        _pageControl.userInteractionEnabled = NO;
    }
    return _pageControl;
}

-(void)reloadLoopScrollView
{
    self.loopScrollView.contentSize = CGSizeMake(self.loopScrollView.frame.size.width*self.imagesGroup.count, self.loopScrollView.frame.size.height);
    for (int i=0; i<self.imagesGroup.count; i++) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.loopScrollView.frame.size.width*i , 0, self.loopScrollView.frame.size.width, self.loopScrollView.frame.size.height)];
        
        /*  1,设置图片，这里用到了SDWebImage加载网络图片    */
        [imgView setImageWithURL:self.imagesGroup[i] placeholderImage:nil options:SDWebImageProgressiveDownload completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
            
        }];
        
        /*  2,如果你的self.imagesGroup数组中存放的是本地图片,则注释掉上面的方法，打开下面的方法     */
//        UIImage *img = self.imagesGroup[i];
//        imgView.image = img;
        [self.loopScrollView addSubview:imgView];
    }
    self.pageControl.numberOfPages = self.imagesGroup.count-2;
    self.pageControl.currentPage = 0;
    [self performSelector:@selector(loopScrollViewAnimated) withObject:nil afterDelay:YHLoopAnimatedTime];
    
}

-(void)loopScrollViewAnimated
{
    _times++;
    if (_times >= YHLoopAnimatedTime) {
        [_loopScrollView setContentOffset:CGPointMake(_loopScrollView.contentOffset.x+self.loopScrollView.frame.size.width, 0) animated:YES];
    }
    [self performSelector:@selector(loopScrollViewAnimated) withObject:nil afterDelay:1];
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _times = 0;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    _times = 0;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView != _loopScrollView) {
        return;
    }
    CGFloat offsetX = scrollView.contentOffset.x;
    if (offsetX <= 0) {
        [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width * (self.imagesGroup.count-2), 0) animated:NO];
    } else if (offsetX >= scrollView.contentSize.width - scrollView.frame.size.width) {
        [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0) animated:NO];
    }
    self.pageControl.currentPage = scrollView.contentOffset.x/scrollView.frame.size.width-1;
    
    if ((int)scrollView.contentOffset.x % (int)scrollView.frame.size.width)
    {
        // 如果刚好到每页的边界, 这样每次如果自己手动滑动，就会重新计算动画时间
        _times = 0;
    }
    
}

-(void)loopScrollViewDidSelectd
{
    CGFloat offsetX = _loopScrollView.contentOffset.x;
    NSInteger index = (offsetX + _loopScrollView.frame.size.width / 2) / _loopScrollView.frame.size.width;//这个index从1开始，并非数组索引
//    NSLog(@"index:%ld",index);
    [self.yhLoopScrollViewDelegate yhLoopScrollView:self.loopScrollView didSelectWithIndex:index/*这个index从1开始，并非数组索引*/];
}


@end
